package structures.recursion;

public class MiGong {
    public static void main(String[] args) {
        // 初始化迷宫
        int[][] miGong = new int[8][7];
        for (int index = 0; index < miGong[0].length; index++) {
            miGong[0][index] = 1;
            miGong[miGong.length - 1][index] = 1;
        }
        for (int index = 0; index < miGong.length; index++) {
            miGong[index][0] = 1;
            miGong[index][miGong[0].length - 1] = 1;
        }
        miGong[miGong.length/2][0] = 1;
        miGong[miGong.length/2][1] = 1;
        miGong[miGong.length/2][2] = 1;
        miGong[miGong.length/2][3] = 1;

        // 显示地图
        for (int[] row : miGong) {
            for (int data : row) {
                System.out.print(String.valueOf(data) + " \t");
            }
            System.out.println();
        }

        setWay(miGong, 1, 1);
        System.out.println("-------------------");

        // 显示地图
        for (int[] row : miGong) {
            for (int data : row) {
                System.out.print(String.valueOf(data) + " \t");
            }
            System.out.println();
        }
    }

    /**
     *  1表示墙  0表示没有涉足过  2表示走过  3表示死路
     * @param map 地图信息
     * @param x 横坐标
     * @param y 纵坐标
     * @return 是否走通
     */
    public static boolean setWay(int[][] map, int x, int y) {
        if (map[6][1] == 2) {
            return true;
        }
        if (map[x][y] == 0) {
            map[x][y] = 2;
            if (setWay(map, x+ 1, y)) { // ↓
                return true;
            } else if (setWay(map, x, y + 1)) { // →
                return true;
            } else if (setWay(map, x - 1, y)) { // ↑
                return true;
            } else if (setWay(map, x, y - 1)) { // ←
                return true;
            } else {
                map[x][y] = 3;
                return false;
            }
        } else {
            return false;
        }
    }
}
